package com.gandong8.trace.tool.util;

import org.springframework.stereotype.Component;

@Component
public class TraceUtil {

    public static final String TRACE_TAG = "TRACE_ID";

    private static String genTraceId() {
        return IdUtil.uuid();
    }

    public static String getTraceId() {
        String traceId = org.slf4j.MDC.get(TRACE_TAG);
        if(traceId == null) {
            return genTraceId();
        }
        return traceId;
    }

    public static void setLogTrace() {
        org.slf4j.MDC.put(TRACE_TAG, genTraceId());
    }

    public static void setLogTrace(String traceId) {
        if(traceId == null) {
            setLogTrace();
        } else {
            org.slf4j.MDC.put(TRACE_TAG, traceId);
        }
    }
}
